﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Concrete;
using System.Configuration;


using Ninject;
using Ninject.Parameters;
using Ninject.Syntax;


namespace SportsStore.WebUI.Infrastructure {

    public class NinjectDependencyResolver : IDependencyResolver {

        private IKernel ninjectKernel;

        public NinjectDependencyResolver() {
            ninjectKernel = new StandardKernel();
            AddBindings();
        }

        public Object GetService(Type serviceType) {
            return ninjectKernel.TryGet(serviceType);
        }

        public IEnumerable<Object> GetServices(Type serviceType) {
            return ninjectKernel.GetAll(serviceType);
        }

        public IBindingToSyntax<T> Bind<T>() {
            return ninjectKernel.Bind<T>();
        }

        public IKernel Kernel {
            get { return ninjectKernel; }
        }

        private void AddBindings() {
            Bind<IProductRepository>().To<EFProductRepository>();

            EmailSettings emailSettings = new EmailSettings {
                WriteAsFile
                    = bool.Parse(ConfigurationManager.AppSettings["Email.WriteAsFile"] ?? "false")
            };

            Bind<IOrderProcessor>().To<EmailOrderProcessor>()
                                   .WithConstructorArgument("emailSettings", emailSettings);

            Bind<IAuthProvider>().To<FormsAuthProvider>();
        }


        
    }
}